//
//  MKUIElementViewImage.m
//  MKUI
//
//  Created by 苏尚进 on 2023/7/13.
//  Copyright © 2023 moon. All rights reserved.
//

#import "MKUIElementViewImage.h"
#import "MKUIPreCompile.h"

@interface MKUIElementViewImage()
@property(nonatomic,strong) UIImageView *detailImageView;
@end

@implementation MKUIElementViewImage
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        _detailImageSize = CGSizeMake(28, 28);
        self.detailImageView = [[UIImageView alloc] init];
        self.detailImageView.contentMode = UIViewContentModeScaleAspectFit;
        self.detailImageView.hidden = self.detailImage==nil;
        [self.contentView addSubview:self.detailImageView];
        //
        @MK_WEAKIFY(self);
        self.detailImageViewLayoutConstraint = [MKUILayoutConstraintItemWrapper wrapItem:self.detailImageView sizeThatFitsBlock:^CGSize(MKUILayoutConstraintItemWrapper * _Nonnull wrapper, CGSize size, BOOL resizeItems) {
            @MK_NORMALIZE(self);
            return self.detailImageSize;
        }];
        self.detailImageViewLayoutConstraint.margin = self.detailImageMarggin;
        //
        self.segLayoutConstraint.isLayoutPriorityFirstItems = NO;
        [self.segLayoutConstraint addItem:self.detailImageViewLayoutConstraint];
    }
    return self;
}
- (void)setDetailImage:(UIImage *)detailImage{
    if(_detailImage==detailImage)return;
    _detailImage = detailImage;
    self.detailImageView.image = self.detailImage;
    self.detailImageView.hidden = self.detailImage==nil;
    [self setNeedsLayout];
}
- (void)setDetailImageMarggin:(UIEdgeInsets)detailImageMarggin{
    if(UIEdgeInsetsEqualToEdgeInsets(_detailImageMarggin, detailImageMarggin))return;
    _detailImageMarggin = detailImageMarggin;
    self.detailImageViewLayoutConstraint.margin = self.detailImageMarggin;
    [self setNeedsLayout];
}
- (void)setDetailImageSize:(CGSize)detailImageSize{
    if(CGSizeEqualToSize(_detailImageSize, detailImageSize))return;
    _detailImageSize = detailImageSize;
    [self setNeedsLayout];
}
@end

@implementation MKUIElementListViewCellImage
+ (Class)elementViewClass{
    return MKUIElementViewImage.class;
}
@end

@implementation MKUIElementTableViewCellModelImage
- (void)setEl_detailImage:(UIImage *)el_detailImage{
    self[@"el_detailImage"] = el_detailImage;
    self.el_elementView.detailImage = el_detailImage;
}
- (UIImage *)el_detailImage{
    return self[@"el_detailImage"];
}
- (void)setEl_detailImageMarggin:(UIEdgeInsets)el_detailImageMarggin{
    self[@"el_detailImageMarggin"] = [NSValue valueWithUIEdgeInsets:el_detailImageMarggin];
}
- (UIEdgeInsets)el_detailImageMarggin{
    return [self el_detailImageMargginWithOtherWise:UIEdgeInsetsZero];
}
- (UIEdgeInsets)el_detailImageMargginWithOtherWise:(UIEdgeInsets)others{
    NSValue *value = [self mk_NSValueForKeyPath:@"el_detailImageMarggin"];
    if(!value) return others;
    return value.UIEdgeInsetsValue;
}
- (void)setEl_detailImageSize:(CGSize)el_detailImageSize{
    self[@"el_detailImageSize"] = [NSValue valueWithCGSize:el_detailImageSize];
    self.el_elementView.detailImageSize = el_detailImageSize;
}
- (CGSize)el_detailImageSize{
    return [self el_detailImageSizeWithOtherWise:CGSizeZero];
}
- (CGSize)el_detailImageSizeWithOtherWise:(CGSize)others{
    NSValue *value = [self mk_NSValueForKeyPath:@"el_detailImageSize"];
    if(!value) return others;
    return value.CGSizeValue;
}
@end

@implementation MKUIElementTableViewCellImage
+ (Class)elementViewClass{
    return [MKUIElementViewImage class];
}
- (void)customReloadCellModel{
    [super customReloadCellModel];
    MKUIElementTableViewCellModelImage *cm = self.cellModel;
    MKUIElementViewImage *elementView = self.elementView;
    elementView.detailImage = cm.el_detailImage;
    elementView.detailImageSize = [cm el_detailImageSizeWithOtherWise:elementView.detailImageSize];
    elementView.detailImageMarggin = [cm el_detailImageMargginWithOtherWise:elementView.detailImageMarggin];
}
@end
